package com.ocl.www.utils;

import java.sql.*;

/**
 * Created by Administrator on 2016/12/2.
 */
public class JDBCUntil {

    //驱动程序名
    private static final String DRIVER = ResourceBundleUtils.getProperty("jdbc.driver");
    //URL指向要访问的数据库名mydata
    private static final String URL =  ResourceBundleUtils.getProperty("jdbc.url");
    //MySQL配置时的用户名
    private static final String USER =  ResourceBundleUtils.getProperty("jdbc.user");
    //MySQL配置时的密码
    private static final String PASSWORD =  ResourceBundleUtils.getProperty("jdbc.password");

    public static final Connection getConnection() throws SQLException, ClassNotFoundException {

        //声明Connection对象
        Connection con = null;

        //遍历查询结果集
        try {
            //加载驱动程序
            Class.forName(DRIVER);
            //1.getConnection()方法，连接MySQL数据库！！
            con = DriverManager.getConnection(URL, USER, PASSWORD);
            return con;

        } catch (ClassNotFoundException e) {
            //数据库驱动类异常处理
            System.out.println("Sorry,can`t find the Driver!");
            throw  e;
        } catch (SQLException e) {
            //数据库连接失败异常处理
            throw e;

        }  finally {
//            System.out.println("数据库数据成功获取！！");

            return con;
        }
    }

}
